home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-11 | 5.8 KB | 279 lines | [TEXT/CWIE] |
- /*
- File: TMapView.cp
-
- Contains: Class implementing a map location selector
-
- Written by: Arno Gourdol
-
- Copyright: Copyright 1996 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TMapView.h"
-
- #include <Events.h>
- #include <Resources.h>
-
-
-
- // --------------------------------------------------------------------
- // TMapView
- // --------------------------------------------------------------------
-
- TMapView::TMapView(SInt16 mapID, SInt16 hilitedMapID, SInt16 colorMapID,
- SInt16 colorHilitedMapID, SInt16 regionListID) :
- fSelectedRegion(-1)
- {
- // Build bitmap for the normal and hilited map
- fMap = new TBitmap(mapID, 1);
- assert(fMap != NULL);
-
- fHilitedMap = new TBitmap(hilitedMapID, 1);
- assert(fHilitedMap != NULL);
-
- if (mapID == colorMapID)
- {
- fColorMap = fMap;
- }
- else
- {
- fColorMap = new TBitmap(colorMapID, 8);
- assert(fColorMap != NULL);
- }
-
- if (hilitedMapID == colorHilitedMapID)
- {
- fColorHilitedMap = fHilitedMap;
- }
- else
- {
- fColorHilitedMap = new TBitmap(colorHilitedMapID, 8);
- assert(fColorHilitedMap != NULL);
- }
-
- // Initialize the region arrays
- // The regions will be created at idle time
- {
- RgnLResourceHandle rgnLResourceHandle =
- (RgnLResourceHandle) ::Get1Resource('rgnL', regionListID);
-
- fNumRegions = (**rgnLResourceHandle).regionCount;
-
- fRegions = (Region*)NewPtr(sizeof(Region) * fNumRegions);
- assert(fRegions != NULL);
-
- for (int i = 0; i < fNumRegions; i++)
- {
- fRegions[i].pictureID =
- (**rgnLResourceHandle).regions[i].pictureID;
- fRegions[i].region = NULL;
- fRegions[i].value = (**rgnLResourceHandle).regions[i].value;
- }
-
- ::ReleaseResource((Handle)rgnLResourceHandle);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // ~TMapView
- // --------------------------------------------------------------------
-
- TMapView::~TMapView()
- {
- if (fMap != fColorMap)
- delete fColorMap;
- if (fHilitedMap != fColorHilitedMap)
- delete fColorHilitedMap;
- delete fMap;
- delete fHilitedMap;
-
- if (fRegions != NULL)
- {
- for (int i = 0; i < fNumRegions; i++)
- {
- if (fRegions[i].region != NULL)
- DisposeRgn(fRegions[i].region);
- }
- DisposePtr((Ptr)fRegions);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // Draw
- // --------------------------------------------------------------------
-
- void TMapView::Draw(const TDrawContext& drawContext,
- const CRect& frame) const
- {
- TBitmap* backgroundMap;
- TBitmap* hilitedMap;
-
- if ((drawContext.GetDepth() >= 8) ||
- (!drawContext.IsColor() && drawContext.GetDepth() == 4))
- {
- backgroundMap = fColorMap;
- hilitedMap = fColorHilitedMap;
- }
- else
- {
- backgroundMap = fMap;
- hilitedMap = fHilitedMap;
- }
-
- // Draw the non-hilited map (background)
- drawContext.DrawBitmap(backgroundMap, frame);
-
- // Draw the current region as hilited
- if (fSelectedRegion >= 0 && fRegions[fSelectedRegion].region != NULL)
- {
- OffsetRgn(fRegions[fSelectedRegion].region,
- frame.Left(), frame.Top());
- drawContext.DrawBitmap(hilitedMap, frame,
- fRegions[fSelectedRegion].region);
- OffsetRgn(fRegions[fSelectedRegion].region,
- -frame.Left(), -frame.Top());
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // Pulse
- // --------------------------------------------------------------------
- // Hook function called at regular interval
- // At idle time, build the regions, if there are any remaining to be
- // built
-
- void TMapView::Pulse(void)
- {
- for (int i = 0; i < fNumRegions; i++)
- {
- if (fRegions[i].region == NULL)
- {
- fRegions[i].region = NewRgn();
-
- if (fRegions[i].region != NULL)
- {
- TBitmap regionBitmap(fRegions[i].pictureID, 1);
- if (regionBitmap.Lock())
- {
- ::BitMapToRegion(fRegions[i].region,
- (BitMap*) *(regionBitmap.GetPixMapHandle()));
- regionBitmap.Unlock();
- }
- }
- break;
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // FindRegion
- // --------------------------------------------------------------------
- // Returns the region in which "location" is, or -1
-
- SInt16 TMapView::FindRegion(CPoint location)
- {
- SInt16 result = -1;
-
- for (int i = 0; i < fNumRegions; i++)
- {
- if (fRegions[i].region != NULL &&
- ::PtInRgn(location, fRegions[i].region))
- {
- result = i;
- break;
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // SetValue
- // --------------------------------------------------------------------
- // Set the currently selected region
-
- void TMapView::SetValue(SInt16 value)
- {
- fSelectedRegion = -1;
- for (int i = 0; i < fNumRegions; i++)
- {
- if (fRegions[i].value == value)
- {
- fSelectedRegion = i;
- break;
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // GetValue
- // --------------------------------------------------------------------
- // Return currently selected region
-
- SInt16 TMapView::GetValue(void)
- {
- SInt16 result = -1;
- if (fSelectedRegion >= 0)
- {
- result = fRegions[fSelectedRegion].value;
- }
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // MouseDown
- // --------------------------------------------------------------------
-
- void TMapView::MouseDown(CRect bounds)
- {
- do
- {
- SInt16 selectedRegion;
-
- do
- {
- CPoint location;
- ::GetMouse(location);
- location -= bounds.LeftTop();
- selectedRegion = FindRegion(location);
-
- // Give some time to build additional masks if needed
- Pulse();
- } while(selectedRegion == fSelectedRegion && StillDown());
-
-
- // Update the map, using a draw context iterator
- if (selectedRegion != fSelectedRegion)
- {
- fSelectedRegion = selectedRegion;
-
- TDrawContextIterator iterator(bounds);
-
- while (iterator != iterator.end())
- {
- if ((*iterator).Lock())
- {
- Draw(*iterator, bounds);
- (*iterator).Unlock();
- }
- ++iterator;
- }
- }
- } while (StillDown());
- }
-
-